package main

import (
	"embed"
	"fmt"
	"github.com/gin-gonic/gin"
	"io/fs"
	"log"
	"package-tools/file-utils"
)

//go:embed static-files/*
var StaticFiles embed.FS
var StaticFileDir = "."

func main() {

	fmt.Println("查看文件系统")
	lookFile()
	fmt.Println("------------------------------")

	fmt.Println("开始复制")

	targetDir := "C:\\Users\\Administrator\\Desktop\\test"
	err := file_utils.CopyEmbeddedFiles(StaticFiles, StaticFileDir, targetDir)
	if err != nil {
		fmt.Println("error:", err)
		return
	}

	fmt.Println("文件复制完毕")

	fmt.Println("开始启动文件")
	for _, v := range file_utils.FilePathList {
		fmt.Printf("启动文件: %v \n", v)
		file_utils.Start(v)
	}
	fmt.Println("文件启动完毕")

	server()
}

func lookFile() {
	fmt.Println("main start")

	subFs, err := fs.Sub(StaticFiles, "static-files")
	if err != nil {
		log.Fatalf("获取文件系统失败: %v", err)
	}

	fs.WalkDir(subFs, ".", func(path string, d fs.DirEntry, err error) error {
		if err != nil {
			return err
		}
		fmt.Printf("path: %v\n", path)
		fmt.Printf("name: %v\n", d.Name())
		return nil
	})

	if err != nil {
		log.Fatalf("程序运行报错: %v", err)
	}
}

func server() {
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}
